package com.qidian.buy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qidian.buy.goods.entity.ItemCategory;
import com.qidian.buy.goods.mapper.ItemCategoryMapper;
import com.qidian.buy.goods.service.IItemCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2024-05-08
 */
@Service
public class ItemCategoryServiceImpl  implements IItemCategoryService {
    @Autowired
    private ItemCategoryMapper itemCategoryMapper;
    @Override
    public QdResult<List<ItemCategory>> list() {
        //IItemCategoryService categoryService = (IItemCategoryService) AopContext.currentProxy();
        return QdResult.success(this.listByParentId(0L));
    }

    @Override
    public List<ItemCategory> listByParentId(Long parentId) {
        QueryWrapper<ItemCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<ItemCategory> itemCats = itemCategoryMapper.selectList(wrapper);
        if(itemCats == null || itemCats.size() == 0){
            return null;
        }
        //IItemCategoryService categoryService = (IItemCategoryService) AopContext.currentProxy();
        for (ItemCategory itemCat : itemCats){
            itemCat.setChildren(this.listByParentId(itemCat.getCatId()));
        }
        return itemCats;
    }
}
